﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Newtonsoft.Json;
using RN.ERP.Web.Utilities;
using RN.ERP.Web.ModelService;
using System.Web.SessionState;
using System.IO;

namespace RN.ERP.Web.Handlers
{
    /// <summary>
    /// EnterpriseQualityInfo 的摘要说明
    /// </summary>
    public class EnterpriseQualityInfoHandler : BaseHandler
    {
        private EnterpriseService.EnterpriseServiceClient enterpriseServiceClient = new EnterpriseService.EnterpriseServiceClient();
        public override void ProcessRequest(HttpContext context)
        {
            string action = context.Request["action"];
            string data = context.Request["data"];
            string result = "";
            switch (action)
            {
                case "Add":
                    result = Save(data, context);
                    break;
                case "Get":
                    result = Get(data);
                    break;
                case "Edit":
                    result = Edit(data, context);
                    break;
                case "Delete":
                    result = Delete(data);
                    break;
                case "GetForCpp":
                    result = GetForCpp(context);
                    break;
                //case "Upload":
                //    result = Upload(data);
                //    break;
                default: break;
            }
            context.Response.ContentType = "text/plain";
            context.Response.Write(result);
        }

        private string Save(string data,HttpContext context)
        {
            ///begin 判断图片是否上传2013-12-4
            if (!string.IsNullOrEmpty(context.Request.Params["funcCode"]) && !string.IsNullOrEmpty(context.Request.Params["zzGuid"]))
            {
                string zzGuid = context.Request.Params["zzGuid"].Trim();
                string funcCodes = context.Request.Params["funcCode"].TrimEnd(',');
                string result0 = UtilitiesTool.IsFileExists(zzGuid, "EF", funcCodes);
                if (!string.IsNullOrEmpty(result0))
                {
                    return "{\"Result\":false,\"Message\":\"img\"}";
                }
            }//end
            var entities = JsonConvert.DeserializeObject<List<JsonEntity>>(data);
            string corpQualInfo = "";
            foreach (JsonEntity entity in entities)
            {
                switch (entity.key.ToLower())
                {
                    case "tb_corp_quality":
                        corpQualInfo = entity.value.Trim('\"');
                        break;
                    default:
                        break;
                }
            }
            tb_Corp_Quality qual = JsonConvert.DeserializeObject<tb_Corp_Quality>(corpQualInfo);
            List<tb_Corp_Quality> list = new List<tb_Corp_Quality>();
            list.Add(qual);
            string currentAreaCode = UserInfo.CurrentArea;
            var result = enterpriseServiceClient.CorpQualityAdd(qual.CorpUserGuid.ToString(), JsonConvert.SerializeObject(list), true, currentAreaCode);
            return result;
        }

        private string Get(string data)
        {
            if (string.IsNullOrEmpty(data))
            {
                return string.Empty;
            }
            List<QueryCorpQualityInfoCondition> list = new List<QueryCorpQualityInfoCondition>();
            string currentAreaCode = UserInfo==null?"": UserInfo.CurrentArea;
            var qualities = enterpriseServiceClient.GetCompanyQualities(data, JsonConvert.SerializeObject(list), currentAreaCode);
            return qualities;
        }

        private string GetForCpp(HttpContext context)
        {
            string data = context.Request["data"];
            if (string.IsNullOrEmpty(data))
            {
                return string.Empty;
            }
            string currentAreaCode = context.Request["areaCode"];
            var qualities = enterpriseServiceClient.GetCompanyQualities(data, "", currentAreaCode);
            return qualities;
        }

        private string Edit(string data, HttpContext context)
        {
            ///begin 判断图片是否上传2013-12-4
            if (!string.IsNullOrEmpty(context.Request.Params["funcCode"]) && !string.IsNullOrEmpty(context.Request.Params["zzGuid"]))
            {
                string zzGuid = context.Request.Params["zzGuid"].Trim();
                string funcCodes = context.Request.Params["funcCode"].TrimEnd(',');
                string result0 = UtilitiesTool.IsFileExists(zzGuid, "EF", funcCodes);
                if (!string.IsNullOrEmpty(result0))
                {
                    return "{\"Result\":false,\"Message\":\"img\"}";
                }
            }//end
            var entities = JsonConvert.DeserializeObject<List<JsonEntity>>(data);
            string corpQualInfo = "";
            foreach (JsonEntity entity in entities)
            {
                switch (entity.key.ToLower())
                {
                    case "tb_corp_quality":
                        corpQualInfo = entity.value.Trim('\"');
                        break;
                    default:
                        break;
                }
            }
            tb_Corp_Quality qual = JsonConvert.DeserializeObject<tb_Corp_Quality>(corpQualInfo);
            string currentAreaCode = UserInfo.CurrentArea;
            var result = enterpriseServiceClient.CorpQualityEdit(corpQualInfo, currentAreaCode);
            return result;
        }

        private string Delete(string data)
        {
            string currentAreaCode = UserInfo.CurrentArea;
            var result = enterpriseServiceClient.CorpQualityDel(data, currentAreaCode);
            return result;
        }

    }
}